<?php

if(!defined('EXECROOT')) {
	define('EXECROOT','');
}

class lcarsScroller extends GtkHBox {

	const SCROLL_DOWN = 1;
	const SCROLL_UP = 2;

	private $scroll;
	
	public function __construct() {
		parent::__construct();

		$this->scroll = null;
		$this->scrlbox = new GtkScrolledWindow;
		$this->left = new GtkVBox;
		$this->right = new GtkVBox;
		$this->btnup = new lcarsButton(EXECROOT.lcars::UI_DIR.'/lcars-up-01.png',array('id'=>'commnet-scroll-down','c1'=>'#eeeef2','snd'=>lcars::SND_INIT_SCROLL,'hard'=>true));
		$this->btndn = new lcarsButton(EXECROOT.lcars::UI_DIR.'/lcars-down-01.png',array('id'=>'commnet-scroll-down','c1'=>'#eeeef2','snd'=>lcars::SND_INIT_SCROLL,'hard'=>true));
		$this->btnbtm = new lcarsButton(EXECROOT.lcars::UI_DIR.'/lcars-btm-01.png',array('id'=>'commnet-scroll-to-bottom','c1'=>'#b9b9b9','snd'=>lcars::SND_SCROLL_JMP,'hard'=>true));
		$this->btntop = new lcarsButton(EXECROOT.lcars::UI_DIR.'/lcars-top-01.png',array('id'=>'commnet-scroll-to-top','c1'=>'#b9b9b9','snd'=>lcars::SND_SCROLL_JMP,'hard'=>true));		

		$this->scrlbox->set_policy(Gtk::POLICY_NEVER,Gtk::POLICY_NEVER);
		
		$this->btndn->connect_simple_after('button-press-event',array($this,'initScroll'),self::SCROLL_DOWN);
		$this->btndn->connect_simple_after('button-release-event',array($this,'haltScroll'));
		$this->btnup->connect_simple_after('button-press-event',array($this,'initScroll'),self::SCROLL_UP);
		$this->btnup->connect_simple_after('button-release-event',array($this,'haltScroll'));
		$this->btntop->connect_simple_after('button-press-event',array($this,'scrollToTop'));
		$this->btnbtm->connect_simple_after('button-press-event',array($this,'scrollToBottom'));
		
		$this->left->pack_start($this->scrlbox,true,true,0);
		
		$this->right->pack_start($this->btntop,false,false,0);
		$this->right->pack_start($this->btnup,false,false,0);
		
		$this->right->pack_end($this->btnbtm,false,false,0);
		$this->right->pack_end($this->btndn,false,false,0);

		$this->pack_start($this->left,true,true,0);
		$this->pack_start($this->right,false,false,0);
		
		
		$this->show_all();
		return;
	}
	
	public function initScroll($dir,$dist = 2) {
		if(!$this->scroll) {
			$this->scroll = Gtk::timeout_add(5,array($this,'doScroll'),$dir,$dist);
		}
		
		return true;
	}
	
	public function doScroll($dir,$dist) {
		$adj = $this->scrlbox->get_vadjustment();
		$pos = $adj->get_value();

		if($dir == self::SCROLL_DOWN) {
			if($dist == -1) {
				$pos = $adj->upper - $adj->page_size + 1;
			} else {
				$pos += $dist;
			}
			
			if($pos > ($adj->upper - $adj->page_size)) {
				$adj->set_value($adj->upper - $adj->page_size);
				$this->haltScroll();
			} else {
				$adj->set_value($pos);
			}
		} else {
			if($dist == -1) {
				$pos = -1;
			} else {
				$pos -= $dist;
			}
			if($pos < 0) {
				$adj->set_value(0);
				$this->haltScroll();
			} else {
				$adj->set_value($pos);
			}		
		}
	
		return true;
	}
	
	public function haltScroll() {
		if($this->scroll) {
			Gtk::timeout_remove($this->scroll);
			$this->scroll = null;
		}
	}
	
	public function scrollToBottom() {
		lcars::killTime();
		$this->initScroll(self::SCROLL_DOWN,-1);
		return false;
	}

	public function scrollToTop() {
		lcars::killTime();
		$this->initScroll(self::SCROLL_UP,-1);
		return false;
	}

	public function setSize($w,$h) {
		$this->width = $w;
		$this->height = $h;
		$this->set_size_request($w,$h);
		return;
	}	
	
	public function getSize() {
		return array($this->width,$this->height);
	}
	
	public function getPosition() {
		return array($this->putx,$this->puty);
	}

	public function setPosition($fixed,$x,$y,$move = false) {
		$this->putx = $x;
		$this->puty = $y;
		if($move) {
			$fixed->move($this,$x,$y);
		} else {
			$fixed->put($this,$x,$y);
		}
		return;
	}

}

?>